import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:sl_flutter/pages/mine/mine.dart';
import 'package:sl_flutter/pages/task/task.dart';
import 'package:sl_flutter/utils/http.dart';
import 'package:sl_flutter/utils/tokenManager.dart';

class Tabs extends StatefulWidget {
  Tabs({
    super.key,
  });

  @override
  State<Tabs> createState() => _TabsState();
}

class _TabsState extends State<Tabs> with SingleTickerProviderStateMixin {
  late TabController _tabController;
  var index = 0;

  @override
  void initState() {
    // 如果没有token，跳转到登录页面
    checkToken();
    super.initState();
    _tabController = TabController(length: 3, vsync: this);
    _tabController.addListener(() {
      // 获取当前选中 Tab 的索引
      int currentIndex = _tabController.index;
      setState(() {
        index = currentIndex;
      });
      if (index == 0) {
        // getTaskList();
      }
      // print('当前选中的 Tab 索引是: $currentIndex');
    });
  }

  // 检查 token 的方法
  Future<void> checkToken() async {
    String? token = await TokenManager().getToken();
    if (token == null || token.isEmpty) {
      // 如果 token 为空，跳转到登录页
      if (context.mounted) {
        // 跳转到主页并清空导航栈
        if (context.mounted) {
          Navigator.pushNamedAndRemoveUntil(
              context, '/login', (route) => false);
        }
      }
    }
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: TabBarView(
        controller: _tabController,
        children: [
          // 任务页面
          Center(child: DeliveryStatusPage()),
          // 消息页面
          Center(child: Text('消息页面内容')),
          // 我的页面
          Center(
              child: Mine(
            token: TokenManager().getToken().toString(),
          )),
        ],
      ),
      bottomNavigationBar: Material(
          child: Container(
        height: 60,
        child: TabBar(
          controller: _tabController,
          indicator: const BoxDecoration(),
          tabs: [
            Column(
              children: [
                SvgPicture.asset(
                  'assets/images/ic_tab_btn_task.svg',
                  color: index == 0 ? Colors.red : null,
                  height: 30,
                  width: 30,
                ),
                Text('任务'),
              ],
            ),
            Column(children: [
              SvgPicture.asset(
                'assets/images/ic_tab_btn_mess_nor.svg',
                color: index == 1 ? Colors.red : null,
                height: 30,
                width: 30,
              ),
              Text('消息'),
            ]),
            Column(
              children: [
                SvgPicture.asset(
                  'assets/images/ic_tab_btn_mine_nor.svg',
                  color: index == 2 ? Colors.red : null,
                  height: 30,
                  width: 30,
                ),
                Text('我的'),
              ],
            )
          ],
          indicatorColor: Colors.red,
          labelColor: Colors.red,
          unselectedLabelColor: Colors.grey,
        ),
      )),
    );
  }
}
